"""MySQL密码管理器模块"""

import os
from typing import Dict, Optional, List, Tuple
from src.business.db_manager import db_manager
from src.data.storage import password_storage

class MySQLPasswordManager:
    """MySQL密码管理类"""
    
    def __init__(self, storage_path: str = "~/.mysqlpass/passwords.json"):
        """初始化密码管理器"""
        # 扩展路径中的~为用户主目录
        self.storage_path = os.path.expanduser(storage_path)
        # 使用新的存储和数据库管理类
        password_storage.set_storage_path(self.storage_path)
    
    def add_password(self, name: str, host: str, port: str, username: str, password: str) -> bool:
        """添加或更新MySQL连接密码"""
        return password_storage.add_connection(name, host, port, username, password)
    
    def get_password(self, name: str) -> Optional[Dict[str, str]]:
        """获取MySQL连接密码"""
        return password_storage.get_connection(name)
    
    def list_passwords(self) -> list:
        """列出所有保存的密码名称"""
        return password_storage.list_connections()
    
    def delete_password(self, name: str) -> bool:
        """删除MySQL连接密码"""
        return password_storage.delete_connection(name)
    
    def test_connection(self, name: str) -> Tuple[bool, str]:
        """测试MySQL连接是否可用"""
        return db_manager.test_connection(name)
    
    def get_databases(self, name: str) -> Tuple[bool, List[str] or str]:
        """获取MySQL连接下的所有数据库"""
        return db_manager.get_databases(name)
    
    def execute_query(self, name: str, database: str, query: str) -> Tuple[bool, List[Dict] or str, List[str] or None]:
        """执行SQL查询"""
        return db_manager.execute_query(name, database, query)

# 创建一个默认的密码管理器实例
password_manager = MySQLPasswordManager()